﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
//using PDAMain.BLL;

namespace PDAMain.Forms.RepairWork
{
    public partial class RepairWorkSelect : Form
    {
        public RepairWorkSelect()
        {
            InitializeComponent();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            //查询
            BindData();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //返回
            RepairWorkList list = new RepairWorkList();
            list.Show();
            this.Close();
        }

        private void BindData()
        {
            //处理状态
            string strType = string.Empty;
            //时间
            string strTime = Convert.ToDateTime("1900-01-01").ToString("yyyy-MM-dd"); //初始化時間為1900年

            if (rbPending.Checked == true)
                strType = "1";
            else if (rbProcessed.Checked == true)
                strType = "9";

            if (cbOneDay.Checked == true)
                strTime = DateTime.Now.ToString("yyyy-MM-dd");
            if (cbThreeDay.Checked == true)
                strTime = DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd");
            if(cbOneWeek.Checked==true)
                strTime = DateTime.Now.AddDays(-6).ToString("yyyy-MM-dd");

            //獲取數據
            DataTable dt1 = new DataTable();
            DataSet ds = null;
           // try
            //{
               
                //RepairWorkListDao worklist = new RepairWorkListDao();
                //ds = worklist.select(strType, strTime);
                ds = DataHelper.RepairWorkDao.select(strType, strTime);
          //  }
           /*// catch
           // {
                string str_Sql = "select WONUM as woNum,LOCDESCRIPTION as locDescription,DESCRIPTION as description,case STATUS when '1' then '处理中' when '2' then '转单' when '3' then '报修' when '9' then '已处理' else '无法识别' end as status,STATUSDATE from repairWorkList where STATUS='" + strType + "'";
                ds = PublicMethods.SelectData(str_Sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                    {
                        if (Convert.ToDateTime(ds.Tables[0].Rows[index]["STATUSDATE"]) >= Convert.ToDateTime(strTime))
                        {
                            ds.Tables[0].Rows.RemoveAt(index);
                        }
                    }
                }
            }*/
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                QueryGrid.Visible = false;
                QueryGrid.TableStyles.Clear();
                QueryGrid.Refresh();
                MessageBox.Show("无数据");
                return;
            }

            dt1 = ds.Tables[0];

            for (int k = 0; k < dt1.Rows.Count; k++)
            {
                if (dt1.Rows[k]["status"].ToString() == "9") dt1.Rows[k]["status"] = "已处理";
                else if (dt1.Rows[k]["status"].ToString() == "1") dt1.Rows[k]["status"] = "待处理";
                else if (dt1.Rows[k]["status"].ToString() == "2") dt1.Rows[k]["status"] = "转单";
                else if (dt1.Rows[k]["status"].ToString() == "3") dt1.Rows[k]["status"] = "报修";
                else if (dt1.Rows[k]["status"].ToString() == "4") dt1.Rows[k]["status"] = "待料";
                else if (dt1.Rows[k]["status"].ToString() == "5") dt1.Rows[k]["status"] = "领料";
                else dt1.Rows[k]["status"] = "未定义状态";
            }
            //綁定數據
            QueryGrid.DataSource = dt1;
            DataGridTableStyle dts = new DataGridTableStyle();
            dts.MappingName = dt1.TableName;

            DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
            col1.HeaderText = "工单编号";
            col1.MappingName = "woNum";
            col1.Width = 83;
            dts.GridColumnStyles.Add(col1);
            
            DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
            col2.HeaderText = "地址";
            col2.MappingName = "locDescription";
            col2.Width = 100;
            dts.GridColumnStyles.Add(col2);

            DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
            col3.HeaderText = "描述";
            col3.MappingName = "description";
            col3.Width = 71;
            dts.GridColumnStyles.Add(col3);

            DataGridTextBoxColumn col4 = new DataGridTextBoxColumn();
            col4.HeaderText = "状态";
            col4.MappingName = "status";
            col4.Width = 60;
            dts.GridColumnStyles.Add(col4);
            /**/
            QueryGrid.TableStyles.Clear();
            QueryGrid.TableStyles.Add(dts);
            QueryGrid.Refresh();
        }

        private void rbPending_Click(object sender, EventArgs e)
        {
            rbProcessed.Checked = false;
        }

        private void rbProcessed_Click(object sender, EventArgs e)
        {
            rbPending.Checked = false;
        }

        private void cbOneDay_CheckStateChanged(object sender, EventArgs e)
        {
            if (cbOneDay.Checked == true)
            {
                cbThreeDay.Checked = false;
                cbOneWeek.Checked = false;
            }
        }

        private void cbThreeDay_CheckStateChanged(object sender, EventArgs e)
        {
            if (cbThreeDay.Checked == true)
            {
                cbOneDay.Checked = false;
                cbOneWeek.Checked = false;
            }
        }

        private void cbOneWeek_CheckStateChanged(object sender, EventArgs e)
        {
            if (cbOneWeek.Checked == true)
            {
                cbOneDay.Checked = false;
                cbThreeDay.Checked = false;
            }
        }
    }
}